/*
 *  Medieval Melee
 *  Copyright (C) 2009 Ben Foote
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Uzebox is a reserved trade mark
*/

#include "q.h"

void Q_Init(QUEUE_TYPE *tQueueType, unsigned char *ucQueue)
{
    tQueueType->ucFront = 0;
    tQueueType->ucRear  = 0;
    
    for(unsigned char i = 0; i < MAX_Q_SIZE; i++)
    {
        ucQueue[i] = 0;
    }
}

bool Q_Enqueue(QUEUE_TYPE *tQueueType, unsigned char *ucQueue, unsigned char ucItem)
{    
    if(Q_IsFull(tQueueType, ucQueue) == true)
    {
        return false;
    }
    else
	{
        tQueueType->ucRear++;
        tQueueType->ucRear = (tQueueType->ucRear%MAX_Q_SIZE);
        
        ucQueue[tQueueType->ucRear] = ucItem;
        return true;
	}
}

unsigned char Q_Dequeue(QUEUE_TYPE *tQueueType, unsigned char *ucQueue)
{
    if(Q_IsEmpty(tQueueType, ucQueue) == true)
    {
        return 0;
    }
    else
    {
        unsigned char ucReturn;
        
        tQueueType->ucFront++;
        tQueueType->ucFront = tQueueType->ucFront%MAX_Q_SIZE;
        
        ucReturn = ucQueue[tQueueType->ucFront];
        ucQueue[tQueueType->ucFront] = 0;
                
        return ucReturn;
    }
}

bool Q_IsEmpty(QUEUE_TYPE *tQueueType, unsigned char *ucQueue)
{
    if((tQueueType->ucFront == tQueueType->ucRear) && (ucQueue[tQueueType->ucFront] == 0))
	{        
        return true;
	}
	else
	    return false;
}

bool Q_IsFull(QUEUE_TYPE *tQueueType, unsigned char *ucQueue)
{
    if((tQueueType->ucFront == tQueueType->ucRear) && (ucQueue[tQueueType->ucFront] != 0))
	{        
        return true;
	}
	else
	    return false;
}

